package com.game.fivestone;

public class GameBoard {
    public static final int EMPTY = 0;
    public static final int BLACK = 1;
    public static final int WHITE = 2;

    private final int size;
    private final int[][] board;

    public GameBoard(int size) {
        this.size = size;
        this.board = new int[size][size];
    }

    public void placeStone(int x, int y, int stone) {
        board[y][x] = stone;
    }

    public int getStone(int x, int y) {
        return board[y][x];
    }

    public boolean isEmpty(int x, int y) {
        return board[y][x] == EMPTY;
    }

    public boolean checkWin(int x, int y) {
        int stone = board[y][x];

        // 检查横向
        int count = 1;
        for (int i = 1; i < 5; i++) {
            if (x + i < size && board[y][x + i] == stone) {
                count++;
            } else {
                break;
            }
        }
        for (int i = 1; i < 5; i++) {
            if (x - i >= 0 && board[y][x - i] == stone) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) return true;

        // 检查纵向
        count = 1;
        for (int i = 1; i < 5; i++) {
            if (y + i < size && board[y + i][x] == stone) {
                count++;
            } else {
                break;
            }
        }
        for (int i = 1; i < 5; i++) {
            if (y - i >= 0 && board[y - i][x] == stone) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) return true;

        // 检查左上到右下对角线
        count = 1;
        for (int i = 1; i < 5; i++) {
            if (x + i < size && y + i < size && board[y + i][x + i] == stone) {
                count++;
            } else {
                break;
            }
        }
        for (int i = 1; i < 5; i++) {
            if (x - i >= 0 && y - i >= 0 && board[y - i][x - i] == stone) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) return true;

        // 检查左下到右上对角线
        count = 1;
        for (int i = 1; i < 5; i++) {
            if (x + i < size && y - i >= 0 && board[y - i][x + i] == stone) {
                count++;
            } else {
                break;
            }
        }
        for (int i = 1; i < 5; i++) {
            if (x - i >= 0 && y + i < size && board[y + i][x - i] == stone) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) return true;

        return false;
    }
}